
var Events = (function(){
	
	function handleGrid() {

		var renderCover = function (v, r, i) {
			return ' <a href="' + siteURL('events/appimage/events/' + r.e_filename) + '" target="_blank"><i class="icon-picture"></i> ' + v + '</a>';
		};
		var renderDate = function (v, r, i) {
			return moment(v).format('dddd, MMMM Do YYYY');
		};
		
		$('#grid').datagrid({
			url: siteURL('events/load_data'),
			title: 'Events List',
			height: 400,
			rownumbers: true,
			pagination: true,
			pagePosition: 'top',
			autoRowHeight: false,
			checkbox: true,
			singleSelect: true,
			columns: [[
				{field: 'ck', checkbox: true},
				{field: 'e_name', title: 'Title', width: 180, sortable: true},
				{field: 'e_created_date', title: 'Created', width: 150, sortable: true, align: 'center',formatter: renderDate},
				{field: 'e_modified_date', title: 'Last Edited', width: 150, sortable: true, align: 'center',formatter: renderDate},
				{field: 'e_venue', title: 'Venue', width: 180, sortable: true},
			]]
		});

		var pager = $('#grid').datagrid('getPager');
		pager.pagination({
			buttons: [
				{text: '<i class="icon-plus"></i> Add',  handler: addRecord},
				{text: '<i class="icon-pencil"></i> Edit', handler: editRecord},
				{text: '<i class="icon-trash"></i> Delete', handler: deleteRecord}
			]
		});

	}

	function handleWindow() {
		$('#win').show();
		$('#win').dialog({
			modal: true,
			width: 725,
			title: 'Events Editor',
			buttons: [
				{
					text: 'Submit',
					handler: function() {
						$('[name="e_desc"]').val($('#htmleditor').code());
						$('#form').form('submit', {
							url: siteURL('events/save_data'),
							success: function() {
								$('#win').dialog('close');
								$('#grid').datagrid('reload');
							}
						});
					}
				},
				{
					text: 'Close',
					handler: function() {
						$('#win').dialog('close');
					}
				}
			]
		});
		$('#win').dialog('close');

		$('form').submit(function(e){
			e.preventDefault();
		});

		$('#htmleditor').summernote({
			height: 200,
			toolbar: [
				['style', ['style']],
				['font', ['bold', 'italic', 'underline', 'clear']],
				['color', ['color']],
				['para', ['ul', 'ol', 'paragraph']],
				['height', ['height']],
				['table', ['table']],
				['insert', ['link', 'picture', 'video']],
				['view', ['fullscreen', 'codeview']],
			]
		});

	}

	function addRecord() {
		$('#win').dialog('open');
		$('#form').form('clear').form('reset');
		$('#htmleditor').code('');
	}

	function editRecord() {
		var record = $('#grid').datagrid('getSelected');
		if ( ! record) {
			$.messager.alert('Warning', 'No record selected', 'warning');
			return;
		}

		$('#win').dialog('open');
		$('#form').form('load', record);
		$('#htmleditor').code(record.e_desc);
	}

	function deleteRecord() {
		var record = $('#grid').datagrid('getSelected');
		if ( ! record) {
			$.messager.alert('Warning', 'No record selected', 'warning');
			return;
		}

		$.messager.confirm('Confirm', 'Delete selected record?', function(r){
			if (r) {
				$.ajax({
					url: siteURL('events/delete_data'),
					type: 'post',
					dataType: 'json',
					data: record
				})
				.done(function(){
					$('#grid').datagrid('reload');
				});	
			}
		});
	}

	function handleSearch() {
		$('#omnisearch').on('keypress', function(e){
			if (e.keyCode === 13) {
				var query = $(this).val();
				$('#grid').datagrid('load', {
					query: query
				});
			}
		});
	}

	return {
		init: function() {
			handleGrid();
			handleWindow();
			handleSearch();
		}
	};

}(jQuery));

$(document).ready(Events.init);